package saleMarket.service;

import saleMarket.dao.CustomerDao;
import saleMarket.dao.UserDao;
import saleMarket.domain.Customer;
import saleMarket.domain.User;
import saleMarket.domain.authority.Actor;
import saleMarket.service.authority.ActorService;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 徐元坤
 * @version 1.0  12-07-2020
 */

public class CustomerService {
    private CustomerDao customerDao = CustomerDao.getInstance();
    private static CustomerService customerService = new CustomerService();

    public static CustomerService getInstance(){return customerService;}

    public Customer find(Integer id)throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        Customer customer = null;
        try {
            customer = customerDao.find(id,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return customer;
    }

    public Collection<Customer> findAll()throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        Collection<Customer> customers = null;
        try {
            customers = customerDao.findAll(conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return customers;
    }

    public boolean add(Customer customer)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean added = false;
        try {
            Actor actor = ActorService.getInstance().find(1);
            //创建用户对象，密码和用户名取自student字段值no
            User user = new User(null,customer.getNo(), customer.getNo(), actor, null);
            Integer id = UserDao.getInstance().add(user,conn);
            customer.setId(id);
            added = customerDao.add(customer,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
            e.printStackTrace();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return added;
    }

    public boolean update(Customer customer)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean update = false;
        try {
            update = customerDao.update(customer,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
            System.out.println(e);
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return update;
    }

    public boolean delete(Integer id)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean deleted = false;
        try {
            customerDao.delete(id,conn);
            deleted =UserService.getInstance().delete(id,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return deleted;
    }
}
